//
//  InterestDetailViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "InterestDetailViewController.h"
#import "InterestDetailTableViewCell.h"
@interface InterestDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIView * topBarView;
@property (nonatomic, strong)UITableView * tableView;
@end

@implementation InterestDetailViewController(networking)
- (void)getInterestDetailData{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"phone":[UserModel sharedInstanced].mobile,
                           @"investmentid":self.bidID,
                           @"t":timeStr,
                           @"token":[UserModel sharedInstanced].token
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"phone":[UserModel sharedInstanced].mobile,
                               @"investmentid":self.bidID,
                               @"t":timeStr,
                               @"token":[UserModel sharedInstanced].token,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/order/index?api=api1009"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([compareMD5Str isEqualToString:signResult]) {
                NSLog(@"成功");
                if ([jsonData[@"code"] isEqualToString:@"000000"]) {
//                    [self.messageArr removeAllObjects];
//                    [self.messageArr addObjectsFromArray:jsonData[@"body"]];
//                    NSLog(@"messageArr.count:%lu", (unsigned long)self.messageArr.count);
                }
            }else{
                NSLog(@"失败");
//                [self.view makeToast:[NSString stringWithFormat:@"%@", jsonData[@"msg"]] duration:2 position:CSToastPositionCenter style:nil];
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}

@end
@implementation InterestDetailViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self getInterestDetailData];//网络接口
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"按月返息详情";
    //返回按钮
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 24 + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"out"] forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    //构造头部bar
    [self setUpTopBar];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    
}
#pragma mark - init
- (void)setUpTopBar{
    self.topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45/WIDTH_6_SCALE)];
    self.topBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topBarView];
    int labW = (SCREEN_WIDTH - 36/WIDTH_6_SCALE)/3;
    //返息时间
    UILabel * timeLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 0, labW, self.topBarView.height - 1/WIDTH_6_SCALE)];
    [self label:timeLab withFont:DEF_FontSize_12 withText:@"返息时间" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentLeft withView:self.topBarView];
    //进行状态
    UILabel * accountLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 0, labW, timeLab.height)];
    accountLab.centerX = SCREEN_WIDTH/2;
    [self label:accountLab withFont:DEF_FontSize_12 withText:@"进行状态" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:self.topBarView];
    //进行状态
    UILabel * stateLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - labW - 18/WIDTH_6_SCALE, 0, labW, timeLab.height)];
    [self label:stateLab withFont:DEF_FontSize_12 withText:@"进行状态" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentRight withView:self.topBarView];
    //分割线
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, self.topBarView.bottom - 1/WIDTH_6_SCALE, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 1/WIDTH_6_SCALE)];
    
    lineView.backgroundColor = getColor(grayColor);
    [self.topBarView addSubview:lineView];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topBarView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT - self.topBarView.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[InterestDetailTableViewCell class] forCellReuseIdentifier:NSStringFromClass([InterestDetailTableViewCell class])];
    }return _tableView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InterestDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InterestDetailTableViewCell class]) forIndexPath:indexPath];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45/WIDTH_6_SCALE;
}
#pragma mark - action
- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
